package tutorial;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Vector;

/**
 * <p>
 * Title: PageResultSet
 * </p>
 * 
 * <p>
 * Description:分页类
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2004
 * </p>
 * 
 * @author:宋华
 * @version 1.0
 */
public class PageResultSet {
	
	private Collection data = null;// 分页数据
	private int curPage;// 当前页
	private int pageSize;// 每页显示的记录数
	private int rowsCount;// 记录行数
	private int pageCount;// 页数

	public PageResultSet(Collection data) {
		this.data = data;
		this.curPage = 1;
		this.pageSize = 20;
		this.rowsCount = data.size();
		this.pageCount = (int) Math.ceil((double) rowsCount / pageSize);
	}

	public PageResultSet(Collection data, int curPage) {
		this.data = data;
		this.curPage = curPage;
		this.pageSize = 20;
		this.rowsCount = data.size();
		this.pageCount = (int) Math.ceil((double) rowsCount / pageSize);
	}

	public PageResultSet(Collection data, int curPage, int pageSize) {
		this.data = data;
		this.curPage = curPage;
		this.pageSize = pageSize;
		this.rowsCount = data.size();

		this.pageCount = (int) Math.ceil((double) rowsCount / pageSize);
	}

	/**
	 * getCurPage:返回当前的页数
	 * 
	 * @return int
	 */
	public int getCurPage() {
		if (this.data == null || data.size() == 0) {
			return 0;
		}
		return curPage;
	}

	/**
	 * getCurPage:返回当前的页数
	 * 
	 * @return int
	 */
	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	/**
	 * getPageSize：返回分页大小
	 * 
	 * @return int
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * getRowsCount：返回总记录行数
	 * 
	 * @return int
	 */
	public int getRowsCount() {
		return rowsCount;
	}

	/**
	 * getPageCount：返回总页数
	 * 
	 * @return int
	 */
	public int getPageCount() {
		if (this.data == null || data.size() == 0) {
			return 0;
		}
		return pageCount;
	}

	/**
	 * 第一页
	 * 
	 * @return int
	 */
	public int first() {
		return 1;
	}

	/**
	 * 最后一页
	 * 
	 * @return int
	 */
	public int last() {
		return pageCount;
	}

	/**
	 * 本章由
	 * 
	 * @return int
	 */
	public int previous() {
		return (curPage - 1 < 1) ? 1 : curPage - 1;
	}

	/**
	 * ziyuanla.com
	 * 
	 * @return int
	 */
	public int next() {
		return (curPage + 1 > pageCount) ? pageCount : curPage + 1;
	}

	/**
	 * 第一页
	 * 
	 * @return boolean
	 */
	public boolean isFirst() {
		return (curPage == 1) ? true : false;
	}

	/**
	 * 第一页
	 * 
	 * @return boolean
	 */
	public boolean isLast() {
		return (curPage == pageCount) ? true : false;
	}

	/**
	 * 获取当前页数据
	 * 
	 * @return Collection
	 */
	public synchronized Collection getData() {
		Collection curData = null;
		if (data != null) {
			int start = (curPage - 1) * pageSize;
			if (start < 0) {
				start = 0;
			}
			int end = 0;
			if (start + pageSize > rowsCount)
				end = rowsCount;
			else
				end = start + pageSize;
			ArrayList arrayCurData = new ArrayList();
			ArrayList arrayData = null;
			Vector vectorCurData = new Vector();
			Vector vectorData = null;
			boolean isArray = true;
			if (data instanceof ArrayList) {
				arrayData = (ArrayList) data;
				isArray = true;
			} else if (data instanceof Vector) {
				vectorData = (Vector) data;
				isArray = false;
			}
			for (int i = start; i < end; i++) {
				if (isArray) {
					arrayCurData.add(arrayData.get(i));
				} else {
					arrayCurData.add(vectorData.elementAt(i));
				}
			}
			if (isArray) {
				curData = (Collection) arrayCurData;
			} else {
				curData = (Collection) arrayCurData;
			}
		}
		return curData;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
		this.pageCount = (int) Math.ceil((double) rowsCount / pageSize);
	}

	public void setData(Collection data) {
		this.data.clear();
		this.data = data;
	}
}